package com.demo.apparel.controller.uniapp;

import com.demo.apparel.common.Result;
import com.demo.apparel.entity.OrderItem;
import com.demo.apparel.entity.Orders;
import com.demo.apparel.service.uniapp.OrdersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/wx/orders")
@Tag(name = "订单接口", description = "订单信息接口")
public class WxOrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 创建订单（立即购买）
     * @param userId 用户ID
     * @param totalPrice 订单总金额
     * @param orderItems 订单商品列表
     * @return 创建的订单对象
     */
    @PostMapping("/create")
    @Operation(summary = "创建订单", description = "根据用户ID和商品信息创建一个订单，返回创建的订单对象")
    public Result<Orders> createOrder(@RequestAttribute("userId") String userId,
                                      @RequestParam BigDecimal totalPrice,
                                      @RequestBody List<OrderItem> orderItems) {
        Orders order = ordersService.createOrder(userId, totalPrice, orderItems);
        return Result.success(order);
    }

    /**
     * 支付成功，更新订单状态
     * @param orderId 订单ID
     * @param transactionId 支付交易号
     * @return 支付结果提示
     */
    @PostMapping("/paySuccess/{orderId}")
    @Operation(summary = "支付成功", description = "接收支付成功的通知，更新订单状态为已支付，返回支付成功消息")
    public Result<String> paySuccess(@PathVariable String orderId,
                                     @RequestParam String transactionId) {
        if (ordersService.processPayment(orderId, "WeChat", transactionId)) {
            return Result.success("支付成功，订单已完成");
        }
        return (Result<String>) Result.failure("支付失败，订单未更新");
    }

    /**
     * 模拟支付成功
     * @param orderId 订单ID
     * @param paymentTransactionId 支付交易号
     * @return 支付结果
     */
    @PostMapping("/simulatePaymentSuccess/{orderId}")
    @Operation(summary = "模拟支付成功", description = "模拟支付成功并更新订单状态")
    public String simulatePaymentSuccess(@PathVariable String orderId, @RequestParam String paymentTransactionId) {
        boolean result = ordersService.simulatePaymentSuccess(orderId, paymentTransactionId);
        return result ? "支付成功，订单已更新" : "支付失败，订单未更新";
    }

    /**
     * 查询用户订单列表
     * @param userId 用户ID
     * @return 用户的所有订单列表
     */
    @GetMapping("/list/{userId}")
    @Operation(summary = "查询用户订单", description = "根据用户ID查询该用户的所有订单")
    public Result<List<Orders>> getUserOrders(@PathVariable String userId) {
        List<Orders> orders = ordersService.getUserOrders(userId);
        return Result.success(orders);
    }


    /**
     * 根据订单ID生成支付交易号
     * @param orderId 订单ID
     * @return 生成的支付交易号
     */
    @PostMapping("/generatePaymentTransactionId/{orderId}")
    @Operation(summary = "生成支付交易号", description = "根据订单ID生成一个支付交易号并存入数据库")
    public Result<String> generatePaymentTransactionId(@PathVariable String orderId) {
        String paymentTransactionId = ordersService.generatePaymentTransactionId(orderId);
        return Result.success(paymentTransactionId != null ? paymentTransactionId : "订单不存在");
    }


    /**
     * 取消订单
     * @param orderId 订单ID
     * @return 取消结果
     */
    @PostMapping("/cancel/{orderId}")
    @Operation(summary = "取消订单", description = "根据订单ID取消订单，订单状态更新为已取消")
    public Result<String> cancelOrder(@PathVariable String orderId) {
        boolean result = ordersService.cancelOrder(orderId);
        return Result.success(result ? "订单已取消" : "订单取消失败，可能是已支付或订单不存在");
    }


    /**
     * 查询订单详情
     * @param orderId 订单ID
     * @return 订单详情对象
     */
    @GetMapping("/{orderId}")
    @Operation(summary = "查询订单详情", description = "根据订单ID查询订单的详细信息")
    public Result<Orders> getOrderById(@PathVariable String orderId) {
        Orders order = ordersService.getOrderById(orderId);
        return Result.success(order);
    }
}